/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: DailyLifecycle.java 26 2012-08-20 09:44:34Z zhuxiaopeng $
 * $Revision: 26 $
 * $Date: 2012-08-20 17:44:34 +0800 (一, 2012-08-20) $
 * $Author: zhuxiaopeng $
 */
package daily.entity;

import java.util.*;

import javax.persistence.*;

import zhu.framework.entity.lifecycle.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 26 $ - $Date: 2012-08-20 17:44:34 +0800 (一, 2012-08-20) $
 */
public class DailyLifecycle extends AbstractLifecycle<Daily> {

	public DailyLifecycle(final LifecycleContext lifecycleContext) {
		super(lifecycleContext);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see zhu.framework.entity.lifecycle.Lifecycle#getEntityType()
	 */
	@Override
	public Class<Daily> getEntityType() {
		return Daily.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see zhu.framework.entity.lifecycle.Lifecycle#newInstance()
	 */
	@Override
	public Daily newInstance() {
		return new Daily();
	}

	public List<Daily> findByDay(final Date date) {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Daily o ");
		buf.append(" where o.date = :theDate");
		final Query query = getEntityManager().createQuery(buf.toString());
		query.setParameter("theDate", date, TemporalType.DATE);
		return getQueryResultList(query);
	}

	public List<Daily> findByStaff(final Staff staff) {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Daily o ");
		buf.append(" where o.staff = :theStaff ");
		final Query query = getEntityManager().createQuery(buf.toString());
		query.setParameter("theStaff", staff);
		return getQueryResultList(query);
	}

	public List<Daily> findByPeriod(final Staff staff, final Date startDate, final Date endDate) {
		final StringBuilder buf = new StringBuilder();
		buf.append(" select o from Daily o ");
		buf.append(" where o.date >= :theStartDate");
		buf.append(" and o.date <= :theEndDate");
		buf.append(" and o.staff = :theStaff ");
		final Query query = getEntityManager().createQuery(buf.toString());
		query.setParameter("theStartDate", startDate, TemporalType.DATE);
		query.setParameter("theEndDate", endDate, TemporalType.DATE);
		query.setParameter("theStaff", staff);
		return getQueryResultList(query);
	}
}
